package cn.zch.soft.util.date.testcalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
 * @author zchcpy
 * @date 180505
 */
public class TestCalendar {
  public static void main(String[] args) {
    Date d = new Date();
    System.out.println(d);
    //获取东八区时间
    Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
    System.out.println("1 " + c1.get(Calendar.HOUR_OF_DAY));
    System.out.println("2 " + c1.get(Calendar.HOUR));
    /////////////////////////////////////////网上资料 1
    //获取东八区时间
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
    //获取年
    int year = c.get(Calendar.YEAR);
    //获取月份，0表示1月份
    int month = c.get(Calendar.MONTH) + 1;
    //获取当前天数
    int day = c.get(Calendar.DAY_OF_MONTH);
    //获取本月最小天数
    int first = c.getActualMinimum(Calendar.DAY_OF_MONTH);
    //获取本月最大天数
    int last = c.getActualMaximum(Calendar.DAY_OF_MONTH);
    //获取当前小时
    int time = c.get(Calendar.HOUR_OF_DAY);
    //获取当前分钟
    int min = c.get(Calendar.MINUTE);
    //获取当前秒
    int xx = c.get(Calendar.SECOND);
    SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //当前日期
    String curDate = s.format(c.getTime());
    System.out.println("当前时间：" + year + "-" + month + "-" + day + " " + time + ":" + min + ":" + xx);
    System.out.println("第一天和最后天：" + first + "," + last);
    System.out.println("当前日期curDate====：" + curDate);
    /////////////////////////////////////////网上资料 2
    Calendar cal = Calendar.getInstance();
    int year1 = cal.get(Calendar.YEAR);
    //比当前月份少1
    int month1 = cal.get(Calendar.MONTH);
    //date表示日期，day表示天数，所以date与day_of_month相同
    int date = cal.get(Calendar.DATE);
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
    //表示本周的第几天，从周日开始计算
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
    //12小时制
    int hour = cal.get(Calendar.HOUR);
    //24小时制
    int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    int millisecond = cal.get(Calendar.MILLISECOND);
    int maxDate = cal.getActualMaximum(Calendar.DATE);
    System.out.println("现在的年份为:" + year1);
    System.out.println("现在的月份为:" + month1);
    System.out.println("现在的号为:" + date);
    System.out.println("现在的号为:" + dayOfMonth);
    System.out.println("现在是星期:" + dayOfWeek);
    System.out.println("现在过了的天数为:" + dayOfYear);
    System.out.println("现在几点:" + hour);
    System.out.println("现在几点:" + hourOfDay);
    System.out.println("现在几分:" + minute);
    System.out.println("现在几秒:" + second);
    System.out.println("现在几毫秒:" + millisecond);
    System.out.println("本月最后一天是:" + maxDate);
    /*几天前----------------------------------------------------*/
    Calendar calendar1 = Calendar.getInstance();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    calendar1.add(Calendar.DATE, -180);
    String threeDaysAgo = sdf1.format(calendar1.getTime());
    System.out.println(threeDaysAgo);
  }
}
